#include "StdAfx.h"
#include "MyHelp.h"
#include <windows.h>
#include <fstream>
#include <richedit.h>
#include <atlbase.h>
#include <atlconv.h>
using namespace std;

#define MAX_LENGTH_TEXT 200
MyHelp::MyHelp(void)
{
}


MyHelp::~MyHelp(void)
{
}

//////////////////////////////////////////////////////////////////////////
//						Use help.txt as data 
// help.txt:
//		- "action" : the line contains number of lines which described this "action"
//		- "described lines" ( <=200 wchar , not begin with number )
// 
//////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
//Load one Action
//pos argument from array of position
//determine pos by FindPos(char *textAct) function.

BOOL MyHelp::LoadHelp(char *fileName, LPTSTR lpszTextInList, HWND hEditExplain)
{
	fstream file;
	file.open(fileName,ios::in);
	if(!file) return FALSE;
	int count = 0;
	BOOL getData = FALSE;
	BOOL First = TRUE;
	//add code here

	//get pos
	TCHAR buffer[MAX_LENGTH_TEXT];
	CHAR buffer2[MAX_LENGTH_TEXT*2] = {0};
	CHAR *pBuffer2 = buffer2;
	
	wcscpy(buffer, lpszTextInList);
	memcpy(pBuffer2,buffer,MAX_LENGTH_TEXT*2);
	int pos = FindPos(pBuffer2);

	//read file
	while(!file.eof())
	{
		//take line
		char tempLine[MAX_LENGTH_TEXT*2];
		file.getline(tempLine,MAX_LENGTH_TEXT*2,'\n');
		if(tempLine[0]>='0' && tempLine[0]<='9')
			count++;
	

		//check valid data
		if ( count == pos)
			getData = TRUE;

		// if data is valid, then we get data 
		if(getData == TRUE)
		{
			//find number of lines describe "action"
			
			int iNumLineData = arrNumLines[pos];

		

			for ( int j = 0 ; j < iNumLineData; j++)
			{
				
				char oneLine[MAX_LENGTH_TEXT*2] = {0};
				char* pOneLine = oneLine;
				
				file.getline(oneLine,MAX_LENGTH_TEXT*2);
				
				WCHAR printLine[MAX_LENGTH_TEXT] = {0};

				memcpy(printLine,pOneLine,MAX_LENGTH_TEXT*2);
				
				SendMessage(hEditExplain,EM_REPLACESEL,0,(LPARAM)printLine);
				SendMessage(hEditExplain,EM_REPLACESEL,0,(LPARAM)L"\r\n");
			
			}
			//if we get data, we get out of here. Therefore , we use exitCode
			break;
		}
		
			
	}

	file.close();
	return TRUE;
}

//find position of "action" in help.txt
//if not found, return -1
int MyHelp::FindPos(char * textAct)
{
	int len = arrTextAct.size();
	for (int i = 0 ; i < len ; i++)
	{
		if(arrTextAct[i].compare(textAct)==0)
			return i;
	}

	return -1;
}

//get number of lines which described "action" and get begin position of real "action" passing by indent argument
int MyHelp::FindNumLine(char *textAct, int &indent)
{
	int i = 0;
	char *numLineData = new char[11];
	while(textAct[i]>='0' && textAct[i]<='9')
	{
		
		
		numLineData[i] = textAct[i];
		i+=1;
	}
	numLineData[i]='\0';
	indent = i+1;

	

	int iNumLineData = atoi(numLineData); 
	delete numLineData;
	return iNumLineData;
}

BOOL MyHelp::Surf(char *fileName, HWND hListChoice)
{
	fstream file(fileName,ios::in);
	if(!file)
		return FALSE;
	int arrTextActSize = 0;
	//add code here

	//read file
	
	while(!file.eof())
	{
		//take line
		char tempLine[MAX_LENGTH_TEXT*2] = {0};
		char *pTemLine = tempLine;
		file.getline(tempLine,MAX_LENGTH_TEXT*2);
		
		if(tempLine[0]>='0' && tempLine[0]<='9' )
		{
			
			//find number of lines
			int beginPos = 0;
			int iNumLine = FindNumLine(tempLine,beginPos);
		

			//update arrTextAct
			arrTextActSize++;
			arrTextAct.resize(arrTextActSize);
			arrNumLines.resize(arrTextActSize);
			arrTextAct.push_back(pTemLine+beginPos);
			arrNumLines.push_back(iNumLine);

			//add string "action" to listBoxChoice
		
			WCHAR pritLine[MAX_LENGTH_TEXT];
			memcpy(pritLine,pTemLine+beginPos,MAX_LENGTH_TEXT*2);
			
			SendMessage(hListChoice,LB_ADDSTRING,0,(LPARAM)pritLine);
		}
	}

	file.close();
	return TRUE;
}